﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLCompact;

namespace PictureOrganizer3
{
    public partial class AddAuthorForm : Form
    {
        private Author author;
        public Author Author
        {
            get { return author; }
        }

        private Database ctx;
        public AddAuthorForm(Database ctx)
        {
            InitializeComponent();

            this.ctx = ctx;
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            var tb = (TextBox)sender;
            if (ctx.Authors.Any(a => a.Name.Equals(tb.Text)))
            {
                errorProvider.SetError(tb, "Der angegebene Autor existiert bereits.");
                btn_Ok.Enabled = false;
            }
            else
            {
                errorProvider.SetError(tb, String.Empty);
                btn_Ok.Enabled = true;
            }

        }

        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            if (!btn_Ok.Enabled)
                return;

            author = new Author { Id = Guid.NewGuid(), Name = tb_AuthorName.Text };
            ctx.Authors.InsertOnSubmit(author);
            ctx.SubmitChanges();

            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
